﻿Imports Negocio
Imports Utilitario
Imports Entidad
Public Class frmBusquedaPedido

    Dim util As New Utiles

    Dim servicio1 As New PedidoNegocio
    Dim servicio2 As New DocumentoClienteNegocio

    Dim CIA As String = "01"
    Dim SUC_Codigo As String = "01"
    Dim TDOC_Codigo As String = "P"

    Sub insertaRegistroPedido(ds As DataSet, estado As String)
        For i = 0 To ds.Tables(0).Rows.Count - 1
            dgvPedido.Rows.Add(ds.Tables(0).Rows(i)("DCI_Codigo").ToString.Trim, ds.Tables(0).Rows(i)("Fecha").ToString.Trim,
                               ds.Tables(0).Rows(i)("CLI_Descripcion").ToString.Trim, ds.Tables(0).Rows(i)("VEN_DesCorta").ToString.Trim,
                               ds.Tables(0).Rows(i)("MON_Abreviatura").ToString.Trim, ds.Tables(0).Rows(i)("TDO_Abreviatura").ToString.Trim,
                               ds.Tables(0).Rows(i)("CNV_Descripcion").ToString.Trim, ds.Tables(0).Rows(i)("FOP_Descripcion").ToString.Trim, estado)
        Next
    End Sub

    Sub listarPedidos(codEstado As String)
        Dim ds As DataSet = servicio1.obtenerPedidosXFiltro(codEstado, "", "", "", "", "")
        insertaRegistroPedido(ds, codEstado)
    End Sub

    Sub eliminarRegistro(codPedido As String)
        For Each fila As DataGridViewRow In dgvPedido.Rows
            If fila.Cells("Column1").Value = codPedido Then
                dgvPedido.Rows.Remove(fila)
            End If
        Next
    End Sub

    Sub limpiarRegistros(codEstado As String)
        Dim arreglo As New ArrayList
        Dim contador As Integer = 0
        For Each fila As DataGridViewRow In dgvPedido.Rows
            If fila.Cells("Column9").Value = codEstado Then
                arreglo.Add(fila.Cells("Column1").Value)
            End If
        Next
        For Each codigo As String In arreglo
            eliminarRegistro(codigo)
        Next
    End Sub

    Sub operacion(tipo As Integer)
        If tipo = 1 Then
            listarPedidos(EST_Ingresado)
        ElseIf tipo = 2 Then
            listarPedidos(EST_Comprometido)
        ElseIf tipo = 3 Then
            listarPedidos(EST_Observado)
        ElseIf tipo = 4 Then
            listarPedidos(EST_Aprobado)
        End If
    End Sub

    Private Sub chkIngresado_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkIngresado.CheckedChanged
        If chkIngresado.Checked Then
            operacion(1)
        Else
            limpiarRegistros(EST_Ingresado)
        End If
    End Sub

    Private Sub chkComprometido_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkComprometido.CheckedChanged
        If chkComprometido.Checked Then
            operacion(2)
        Else
            limpiarRegistros(EST_Comprometido)
        End If
    End Sub

    Private Sub chkObservado_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkObservado.CheckedChanged
        If chkObservado.Checked Then
            operacion(3)
        Else
            limpiarRegistros(EST_Observado)
        End If
    End Sub

    Private Sub chkAprobado_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkAprobado.CheckedChanged
        If chkAprobado.Checked Then
            operacion(4)
        Else
            limpiarRegistros(EST_Aprobado)
        End If
    End Sub

    Private Sub chkTodos_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkTodos.CheckedChanged
        If chkTodos.Checked Then
            chkIngresado.Checked = True
            chkComprometido.Checked = True
            chkObservado.Checked = True
            chkAprobado.Checked = True
        Else
            chkIngresado.Checked = False
            chkComprometido.Checked = False
            chkObservado.Checked = False
            chkAprobado.Checked = False
        End If
    End Sub

    Sub mostrarPedido(codPedido As String)
        Dim pedido As Pedido = servicio2.obtenerDocumentoCliente(CIA, SUC_Codigo, TDOC_Codigo, codPedido)
        Dim formPedido As New frmDocumento
        formPedido.txtPedido.Text = pedido.DCI_Codigo
        util.enfocar()
        formPedido.ShowDialog()
    End Sub

    Private Sub dgvPedido_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPedido.CellClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            mostrarPedido(dgvPedido.Item("Column1", f).Value)
        End If
    End Sub

End Class